﻿using AutoMapper;
using B.W.passport.Application.DTO;
using B.W.passport.Domain;
using B.W.passport.IBaseRepisitory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;

namespace B.W.passport.Application.AdminService
{
    public class DelProductService : IDelProductService
    {

        private readonly IBaseRepisitory<Product> ProductRepisitory;
        private readonly IMapper mapper;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="productRepisitory"></param>
        /// <param name="mapper"></param>
        public DelProductService(IBaseRepisitory<Product> productRepisitory, IMapper mapper)
        {
            ProductRepisitory = productRepisitory;
            this.mapper = mapper;
        }
      
        /// <summary>
        /// 商品删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<APIResult<int>> DeleteProduct(int id)
        {
            APIResult<int> result = new APIResult<int>();
            result.Code = APIEnumsCode.删除成功;
            result.Message = APIEnumsCode.删除成功.ToString();
            var list = await ProductRepisitory.GetAllAsync(a => a.ProductId == id);

            var entity = list.FirstOrDefault();
            entity.Deletor = false;
            await ProductRepisitory.UpdateDataAsync(entity);
            return new APIResult<int>()
            {
                Code = APIEnumsCode.删除成功,
                Message = APIEnumsCode.删除成功.ToString()

            };
        }

        /// <summary>
        /// 商品修改
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<APIResult<int>> UpdateProduct(ProductDTO product)
        {
            APIResult<int> result = new APIResult<int>();
            result.Code = APIEnumsCode.修改成功;
            result.Message = APIEnumsCode.修改成功.ToString();
            // var list = ProductRepisitory.Find(x => x.ProductId == product.ProductId);
            var list = mapper.Map<Product>(product);
            ProductRepisitory.UpdateDataAsync(list);

            return await Task.FromResult(result);
        }

       
        ///// <summary>
        ///// 商品删除
        ///// </summary>
        ///// <param name="id"></param>
        ///// <returns></returns>
        //public async Task<APIResult<int>> DeleteProduct(int id)
        //{
        //    APIResult<int> result = new APIResult<int>();
        //    var list = await ProductRepisitory.GetAllAsync(a => a.ProductId == id);
        //    var entity = list.FirstOrDefault();
        //    if (entity == null)
        //    {
        //        result.Code = APIEnumsCode.删除失败; // 假设存在这样一个枚举值表示未找到商品
        //        result.Message = APIEnumsCode.删除失败.ToString();
        //        return result;
        //    }
        //    entity.Deletor = true;
        //    try
        //    {
        //        await ProductRepisitory.UpdateDataAsync(entity);
        //        result.Code = APIEnumsCode.删除成功;
        //        result.Message = APIEnumsCode.删除成功.ToString();
        //    }
        //    catch (Exception ex)
        //    {
        //        result.Code = APIEnumsCode.删除失败; // 假设存在这样一个枚举值表示删除失败
        //        result.Message = $"删除失败，原因：{ex.Message}";
        //    }
        //    return result;
        //}



    }
}
